package com.tmp;

import com.google.common.collect.Lists;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author: chenhs
 * @date: Created in 10:54 2021/6/10
 **/
public class CompareTest {


    public static void main(String[] args) {

        List<String> list = Lists.newArrayList();
        list.add("2021-05-09 21:12:10");
        list.add("2021-05-19 21:12:10");
        list.add("2021-04-29 21:12:10");
        list.add("2021-06-05 21:12:10");



        Collections.sort(list, new Comparator<String>() {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            @Override
            public int compare(String o1, String o2) {
                try {
//                    return f.parse(o1).compareTo(f.parse(o2));
                    return f.parse(o2).compareTo(f.parse(o1));
                } catch (ParseException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });

        System.out.println(list);
    }
}
